package com.wang.mandkey.activity;

import java.util.List;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.wang.mandkey.App;
import com.wang.mandkey.R;
import com.wang.mandkey.adapter.RecommandAdapter;
import com.wang.mandkey.tool.Tool;
import com.waps.AdInfo;
import com.waps.AppConnect;

public class RecommandAPpListActivity extends BaseActivity {

	private ListView mListViewAd;
	private List<AdInfo> mArrayListAd;
	private ProgressDialog mProgressDialog = null;
	private GetAdTask mGetAdTask;
	private ImageView mImageViewRfresh;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}

	private void initView() {
		setContentView(R.layout.recommand);
		mListViewAd = (ListView) findViewById(R.id.listViewRecommand);
		mImageViewRfresh = (ImageView) findViewById(R.id.imageViewRefresh);
		mImageViewRfresh.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mGetAdTask = new GetAdTask();
				mGetAdTask.execute();
			}
		});

		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setTitle(this.getResources().getString(
				R.string.setting_activity_setting_linking));
		mProgressDialog.setCancelable(false);
		mProgressDialog.setMessage(this.getResources().getString(
				R.string.setting_activity_setting_link_net));
		mProgressDialog.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
				if (null != mGetAdTask) {
					mGetAdTask.cancel(true);
				}
			}
		});
		mListViewAd.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					App.getAppConnect().clickAd(
							mArrayListAd.get(position).getAdId());
				} catch (Exception e) {
					e.printStackTrace();
					Log.e(tag, "ad list exeption");
				}

			}

		});
		mGetAdTask = new GetAdTask();
		mGetAdTask.execute();
	}

	public void updateList() {
		if (null != mArrayListAd) {
			RecommandAdapter rca = new RecommandAdapter(this, mArrayListAd);
			mListViewAd.setAdapter(rca);
		}
	}

	class GetAdTask extends AsyncTask<Void, Void, Integer> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgressDialog.show();
		}

		@SuppressWarnings("unchecked")
		@Override
		protected Integer doInBackground(Void... params) {
			try {
				AppConnect appConnector = AppConnect
						.getInstance(RecommandAPpListActivity.this);
				mArrayListAd = appConnector.getAdInfoList();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (null == mArrayListAd) {
				return 0;
			} else {
				return mArrayListAd.size();
			}
		}

		@Override
		protected void onPostExecute(Integer result) {
			mProgressDialog.dismiss();
			if (0 == result) {
				Tool.ToastShow(RecommandAPpListActivity.this, "亲,网络错误,请点击刷新重试!");
			} else {

			}
			updateList();
		}

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
		return;
	}

}
